|
A hierarchical query is a type of SQL query that handles hierarchical model data. They are special case of more general recursive fixpoint queries, which compute transitive closures. In standard SQL:1999 hierarchical queries are implemented by way of recursive ''common table expressions'' (CTEs). Unlike the Oracle extension described below, the recursive CTEs were designed with fixpoint semantics from the beginning. The recursive CTEs from the standard were relatively close to the existing implementation in IBM DB2 version 2.〔 Recursive CTEs are also supported by Microsoft SQL Server,〔(【引用サイトリンク】title=Recursive Queries Using Common Table Expressions )〕 Firebird 2.1, PostgreSQL 8.4+,〔 PostgreSQL〕 Oracle 11g Release 2 and CUBRID. An alternative syntax is the non-standard CONNECT BY construct; it was introduced by Oracle in the 1980s. Prior to Oracle 10g, the construct was only useful for traversing acyclic graphs because it returned an error on detecting any cycles; in version 10g Oracle introduced the NOCYCLE feature (and keyword), making the traversal work in the presence of cycles as well.Without Common-table-expressions or a connected-by clause it is possible to achieve hierarchical queries with user-defined recursive functions.〔(Paragon corporation: Using PostgreSQL User-Defined Functions to solve the Tree Problem ), February 15, 2004, accessed September 19, 2015〕 == CONNECT BY == CONNECT BY is supported by EnterpriseDB,〔(Hierarchical Queries ), EnterpriseDB〕 Oracle database,〔(Hierarchical Queries ), Oracle〕 CUBRID,〔(【引用サイトリンク】url=http://www.cubrid.org/manual/841/en/Hierarchical%20Query )〕 and DB2 although only if it is enabled as a compatibility mode. The syntax is as follows:;For example: The output from the above query would look like: level | employee | empno | manager -------+-------------+-------+--------- 1 | KING | 7839 | 2 | JONES | 7566 | 7839 3 | SCOTT | 7788 | 7566 4 | ADAMS | 7876 | 7788 3 | FORD | 7902 | 7566 4 | SMITH | 7369 | 7902 2 | BLAKE | 7698 | 7839 3 | ALLEN | 7499 | 7698 3 | WARD | 7521 | 7698 3 | MARTIN | 7654 | 7698 3 | TURNER | 7844 | 7698 3 | JAMES | 7900 | 7698 2 | CLARK | 7782 | 7839 3 | MILLER | 7934 | 7782 (14 rows) 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Hierarchical and recursive queries in SQL」の詳細全文を読む スポンサード リンク
|